﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
public partial class Pub_Module_J_MyOrder : System.Web.UI.UserControl
{
    clsCommon clsCom = new clsCommon();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
            Bind_Order();
    }

    private void Bind_Order()
    {
        string UserID = Request.QueryString["UserID"];
        if (Request.QueryString["type"] == "IsAdmin")
        {
            if (Session["AdminUserID"] == null || Session["AdminUserID"].ToString() == "")
            {
                Response.Write("<script language=javascript>alert('Time is out.');window.open('" + ResolveUrl("~/") + "Home.aspx','_top');</script>");
                return;
            }
            else
            {
                clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Request.QueryString["CodeID"]);
                if (clsAuth.IsShowAuth() == true || Session["IsAdmin"].ToString() == "S")
                {
                    grvMaster.Columns[0].Visible = false;
                }
                else
                {
                    Response.Write("<script language=javascript>alert('Time is out.');window.open('" + ResolveUrl("~/") + "Home.aspx','_top');</script>");
                    return;

                }

            }
        }
        else
        {
           
            if (Session["UserID"] == null)
                Response.Redirect("~/Member/Login/Login.aspx");
            UserID = Session["UserID"].ToString();
        }
        clsWebShop cls = new clsWebShop();
        //Lay so luong trong kho
        Session["dtCountStore"] = cls.Get_QuantityStoreGroup("", "0").Tables[0];
        string sDateStart, sDateEnd;
        DataTable dt = new DataTable();
        string sDate = "", eDate = "";
        /* if (Picker1.SelectedDate.ToString("yyyyMMdd") != "00010101")
             sDate = Picker1.SelectedDate.ToString("yyyyMMdd");
         if (Picker2.SelectedDate.ToString("yyyyMMdd") != "00010101")
             eDate = Picker2.SelectedDate.ToString("yyyyMMdd");*/

        sDateStart = sDate;
        sDateEnd = eDate;
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("StartDate", Calendar1.Text.Replace("/", ""));
        msg.CreateFirstNode("EndDate", Calendar2.Text.Replace("/", ""));
        msg.CreateFirstNode("Status", "");
        msg.CreateFirstNode("End_MK", "");
        msg.CreateFirstNode("OrderID", cls.killSQLInjection(txtOrderID.Text));
        msg.CreateFirstNode("UserID", UserID);
        dt = cls.Get_OrderPDOverViewByCondition(msg.GetXmlStr.ToString()).Tables[0];

        BindData(dt);



    }

    private void BindData(DataTable dt)
    {
        grvMaster.DataSource = dt;
        grvMaster.DataBind();

    }
    protected void grvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.DataRow:
                LinkButton lblDelete = (LinkButton)(e.Row.Cells[0].FindControl("lblDelete"));
                lblDelete.Attributes.Add("onclick", "if(confirm('Bạn muốn hủy đơn hàng này???')) return true; return false;");

                Label lblThanhTien = (Label)(e.Row.Cells[0].FindControl("lblThanhTien"));
                lblThanhTien.Text = clsCom.FormatMoney(lblThanhTien.Text);
                Label lblOrderDate = (Label)(e.Row.Cells[0].FindControl("lblOrderDate"));
                lblOrderDate.Text = clsCom.FormatDate(lblOrderDate.Text);
                if (DataBinder.Eval(e.Row.DataItem, "Status").ToString() != "N")
                    lblDelete.Visible = false;

                if (DataBinder.Eval(e.Row.DataItem, "Status").ToString() == "C")
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
                else if (DataBinder.Eval(e.Row.DataItem, "Status").ToString() == "E")
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.Blue;
                else if (DataBinder.Eval(e.Row.DataItem, "Status").ToString() == "S")
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#9966ff");
                else if (DataBinder.Eval(e.Row.DataItem, "Status").ToString() == "Y")
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.YellowGreen;


                //hplOrderID.Text = "<a onmouseover=\"this.style.backgroundColor='LemonChiffon'; this.style.fontSize='13px' \"    onmouseout=\"this.style.backgroundColor='Transparent';this.style.fontSize='12px' \" style='cursor:pointer' href='SignOrderPD.aspx?CodeID=" + Request.QueryString["CodeID"] + "&sDate=" + Picker1.SelectedDate.ToString("yyyyMMdd") + "&eDate=" + Picker2.SelectedDate.ToString("yyyyMMdd") + "&sState=" + ddlState.SelectedValue + "&sOrderID=" + txtDDH.Text + "&OrderID=" + DataBinder.Eval(e.Row.DataItem, "OrderID").ToString() + "&State=" + DataBinder.Eval(e.Row.DataItem, "Status").ToString() + "'><font color='Green'>" + DataBinder.Eval(e.Row.DataItem, "OrderID").ToString().ToUpper() + "</font></a>";

                e.Row.Attributes.Add("onmouseover", "this.className='ItemHover_Basket'");
                e.Row.Attributes.CssStyle.Add("cursor", "pointer");
                e.Row.Attributes.Add("onmouseout", "this.className='ItemOut_Basket'");
                Label lblStatus = (Label)(e.Row.Cells[0].FindControl("lblStatus"));

                clsFunAp clsFun = new clsFunAp();

                lblStatus.Text = clsFun.ReturnStatus(DataBinder.Eval(e.Row.DataItem, "Status").ToString());

                break;
        }
    }



    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind_Order();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtOrderID.Text = "";
        Calendar1.Text = "";
        Calendar2.Text = "";
        Bind_Order();
    }
    protected void grvMaster_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        string sOrderID = grvMaster.DataKeys[e.RowIndex].Value.ToString();
        cls.UpdateData("update OrderPD set UpdID=" + Session["UserID"].ToString() + ", Status='C',EndDate='" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "' where  OrderID='" + sOrderID + "'");

        //INSERT FLOWORDERHIST
        clsFunAp clsFun = new clsFunAp();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("OrderID", sOrderID);
        msg.CreateFirstNode("SignUser", Session["UserID"].ToString());
        msg.CreateFirstNode("Status", "C");
        msg.CreateFirstNode("Note", clsFun.NoteTo_C());

        cls.Ins_Upd_FlowOrderHist(msg.GetXmlStr);


        /*ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Hủy đơn thành công');</script>");*/
        Bind_Order();
    }
    protected void grvMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvMaster.PageIndex = e.NewPageIndex;
        Bind_Order();
    }
}
